package com.lxmes.sales.service.impl;

import java.util.Date;
import java.util.List;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.bean.BeanUtils;
import com.lxmes.sales.domain.Assetfiles;
import com.lxmes.sales.mapper.AssetfilesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.sales.mapper.AssetPdMapper;
import com.lxmes.sales.domain.AssetPd;
import com.lxmes.sales.service.IAssetPdService;

/**
 * 资产盘点Service业务层处理
 *
 * @author lx
 * @date 2024-08-14
 */
@Service
public class AssetPdServiceImpl implements IAssetPdService
{
    @Autowired
    private AssetPdMapper assetPdMapper;
    @Autowired
    private AssetfilesMapper assetfilesMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询资产盘点
     *
     * @param id 资产盘点主键
     * @return 资产盘点
     */
    @Override
    public AssetPd selectAssetPdById(Long id)
    {
        return assetPdMapper.selectAssetPdById(id);
    }

    /**
     * 查询资产盘点列表
     *
     * @param assetPd 资产盘点
     * @return 资产盘点
     */
    @Override
    public List<AssetPd> selectAssetPdList(AssetPd assetPd)
    {
        return assetPdMapper.selectAssetPdList(assetPd);
    }

    /**
     * 新增资产盘点
     *
     * @param assetPd 资产盘点
     * @return 结果
     */
    @Override
    public int insertAssetPd(AssetPd assetPd)
    {
        assetPd.setCreateTime(DateUtils.getNowDate());
        return assetPdMapper.insertAssetPd(assetPd);
    }

    @Override
    public int scAssetPd(AssetPd assetPd) {
        String number= "ZCPD"+ NewNumber.createNewNumber(assetPdMapper.getNewCode(DateUtils.getDate()));
        Assetfiles assetfiles=new Assetfiles();
        List<Assetfiles> list = assetfilesMapper.selectAssetfilesList(assetfiles);
        for (Assetfiles assetfile : list) {
            AssetPd pd=new AssetPd();
            BeanUtils.copyProperties(assetfile,pd);
            pd.setDeptUse(assetfile.getSapProof());
            pd.setDeptCode(assetfile.getReceivingUnit());
            pd.setOrgCode(SecurityUtils.getFactoryCode());
            pd.setOrgName(SecurityUtils.getFactoryName());
            pd.setPlanNum("1");
            pd.setPdNum("1");
            pd.setInventoryStatus("0");
            pd.setPdDanhao(number);
            pd.setCreateBy(SecurityUtils.getUsername());
            pd.setCreateTime(new Date());
            assetPdMapper.insertAssetPd(pd);
        }
        return 1;
    }

    @Override
    public int shAssetPd(AssetPd assetPd) {
        List<Long> idList = assetPd.getIds();
        for (Long id : idList) {
            AssetPd pd = assetPdMapper.selectAssetPdById(id);
            pd.setInventoryStatus("1");
            pd.setPdDate(DateUtils.getDate());
            pd.setPdName(SecurityUtils.getUsername());
            assetPdMapper.updateAssetPd(pd);
        }
        return 1;
    }
    /**
     * 修改资产盘点
     *
     * @param assetPd 资产盘点
     * @return 结果
     */
    @Override
    public int updateAssetPd(AssetPd assetPd)
    {
        return assetPdMapper.updateAssetPd(assetPd);
    }

    /**
     * 批量删除资产盘点
     *
     * @param ids 需要删除的资产盘点主键
     * @return 结果
     */
    @Override
    public int deleteAssetPdByIds(Long[] ids)
    {
        return assetPdMapper.deleteAssetPdByIds(ids);
    }

    /**
     * 删除资产盘点信息
     *
     * @param id 资产盘点主键
     * @return 结果
     */
    @Override
    public int deleteAssetPdById(Long id)
    {
        return assetPdMapper.deleteAssetPdById(id);
    }
    @Override
    public String imporIteminfo(List<AssetPd> assetPdList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(assetPdList) || assetPdList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (AssetPd assetPd : assetPdList)
        {
            try
            {
                BeanValidators.validateWithException(validator, assetPd);
                assetPd.setCreateBy(operName);
                assetPd.setUpdateBy(operName);
                this.insertAssetPd(assetPd);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
